<?php
class Group
{
  private $id;
  private $r_play;
  private $r_download;
  private $r_add_file;
  private $r_mod_own;
  private $r_mod_all;
  private $r_del_own;
  private $r_del_all;
  private $r_dist;
  private $r_admin;
  
  public function __construct($id,$values='')
  {
    $id = trim($id);
    if(!empty($id) and empty($values))
    {
      $this->id = $id;
      $this->read();
    }
    if(!empty($id) and !empty($values))
    {
      $this->id = $id;
      foreach($values as $key=>$value)
      {
        $this->$key = $value;
      }
      $this->update();
    }
  }
  public static function setRight($id,$right,$value)
  {
    $sql = "UPDATE grp set ".sqlite_escape_string($right)."='".sqlite_escape_string($value)."' WHERE id='".sqlite_escape_string($id)."'";
    $dbh = new dbSql();
    $res = $dbh->exec($sql);
    if($res === 1)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  
  private function read()
  {
    $sql = "SELECT * FROM grp WHERE id='".sqlite_escape_string($this->id)."'";
    $dbh = new dbSql();
    $res = $dbh->query($sql);
    if(!empty($res))
    {
      $this->r_play     = $res[0]['r_play'];
      $this->r_download = $res[0]['r_download'];
      $this->r_add_file = $res[0]['r_add_file'];
      $this->r_mod_own  = $res[0]['r_mod_own'];
      $this->r_mod_all  = $res[0]['r_mod_all'];
      $this->r_del_own  = $res[0]['r_del_own'];
      $this->r_del_all  = $res[0]['r_del_all'];
      $this->r_dist     = $res[0]['r_dist'];
      $this->r_admin    = $res[0]['r_admin'];
    }
  }

  public function form()
  {
    $fields = array('r_play'=>STR_UI_R_PLAY,
                    'r_download'=>STR_UI_R_DOWNLOAD,
                    'r_add_file'=>STR_UI_R_ADD_FILE,
                    'r_mod_own'=>STR_UI_R_MOD_OWN,
                    'r_mod_all'=>STR_UI_R_MOD_ALL,
                    'r_del_own'=>STR_UI_R_DEL_OWN,
                    'r_del_all'=>STR_UI_R_DEL_ALL,
                    'r_dist'=>STR_UI_R_DIST,
                    'r_admin'=>STR_UI_R_ADMIN);
    $output = '';
    foreach($fields as $field=>$label)
    {
      if($this->$field == 'T')
      {
        $check = ' checked="checked"';
      }
      else
      {
        $check = '';
      }
      $output.= '<input class="groupCheckRights" value="'.$field.'" onchange="updateGroupRights(this);" type="checkbox"'.$check.' id="'.$field.'"/>&nbsp;<label for="'.$field.'">'.$label.'</label><br/>';
    }
    $html = ' <form action="#" method="post">
                <fieldset>
                  <legend>'.$this->id.'</legend>
                  <input type="hidden" value="'.$this->id.'" id="groupIdValue"/>
                  '.$output.'
                </fieldset>
              </form>';
    return $html;
  }

  public static function createNew($id)
  {
    $id = trim($id);
    
    
    $sql = "INSERT INTO grp VALUES('".sqlite_escape_string($id)."','F','F','F','F','F','F','F','F','F');";
    $dbh = new dbSql();
    try
    {
      $res = $dbh->exec($sql);
    }
    catch(Exception $e)
    {
      return($e->getMessage());
    }
    if($res === 1)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  

  public static function delete($id)
  {
    $dbh = new dbSql();
    
    $sql_verif = "SELECT count(*) FROM usr WHERE grp_id='".sqlite_escape_string($id)."'";
    $res_verif = $dbh->query($sql_verif);
    if($res_verif[0]['count(*)'] > 0)
    {
      return false;
    }
    
    $sql = "DELETE FROM grp WHERE id='".sqlite_escape_string($id)."'";
    $res = $dbh->exec($sql);
    if($res === 1)
    {
      return true;
    }
    else
    {
      return false;
    }
  }

  public static function listGroups($id='')
  {
    $id = trim($id);
    $sql = "SELECT id FROM grp WHERE id!='admin' ORDER BY id";
    $dbh = new dbSql();
    $res = $dbh->query($sql);
    $list['empty'] = STR_HLP_GROUPS_LIST;
    foreach($res as $item)
    {
      $list[$item['id']] = $item['id'];
    }
    $html = '<input type="image" src="app/skin/transparent.gif" class="btnGroupAdd" onmouseover="help(str.hlp_add_group);" onclick="addGroup();"/>&nbsp;'.
    Html::mkSelect(array('id'=>'groupsSelect','selected'=>$id,'script'=>'onchange="groupForm(this.value);"','values'=>$list)).
    '&nbsp;<input type="image" src="app/skin/transparent.gif" class="btnGroupDelete" onmouseover="help(str.hlp_delete_group);" onclick="deleteGroup();"/>'
    ;
    return $html;
  }
  public static function getGroups()
  {
    $sql = "SELECT * FROM grp ORDER BY id";
    $dbh = new dbSql();
    $res = $dbh->query($sql);
    return $res;
  }
}
?>
